<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车</title>
    <link rel="stylesheet" href="./css/reset.css">
    <style>
        body {
            background-color: gainsboro;
        }

        #app {
            width: 320px;
            height: 390px;
            background-color: #fff;
            padding: 0 16px;
            margin: 100px auto;
        }

        .top {
            height: 30px;
            /* background-color: red; */
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .title {
            font-weight: 700;
        }

        .iconfont {
            color: gray;
        }

        .empty {
            font-size: 14px;
            font-weight: 700;
        }

        .item {
            height: 100px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            /* background-color: yellow; */
        }

        .left {
            width: 80px;
            height: 80px;
            background-color: red;
        }

        img {
            width: 100%;
            height: 100%;
        }

        .middle {
            flex: 1;
            margin-left: 10px;
            line-height: 58px;
        }

        .name {
            font-weight: 700;
            font-size: 14px;
        }

        .price {
            color: red;
        }

        .ori_price {
            color: gray;
            font-size: 12px;
            text-decoration: line-through;
        }

        .right {
            align-self: flex-end;
            display: flex;
            margin-bottom: 10px;
        }

        .jian,
        .add {
            width: 20px;
            height: 20px;
            color: #00a6ff;
            border-radius: 50%;
            text-align: center;
            line-height: 20px;
            border: 1px solid #00a6ff;
            cursor: pointer;
        }

        .add {
            color: #fff;
            background-color: #00a6ff;
        }

        .count {
            margin: 0 10px;
        }

        .kong {
            text-align: center;
            line-height: 40px;
            color: red;
        }

        .bottom {
            height: 50px;
            display: flex;
            justify-content: space-around;
            line-height: 50px;
            background-color: rgba(128, 128, 128, 0.252);
        }

        .jiage {
            font-size: 12px;
        }

        .ori_totalPrice {
            color: gray;
            text-decoration: line-through;
        }

        .totalPrice {
            font-size: 16px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="top">
            <div class="title">已选商品</div>
            <div class="clear">
                <span class="iconfont ">#</span>
                <span class="empty" @click="empty">清空</span>
            </div>
        </div>
        <div class="list">
            <div class="item" v-for="(item,index) in tasklist" v-bind:key="index">
                <div class="left">
                    <img v-bind:src="item.pic" alt="">
                </div>
                <div class="middle">
                    <div class="name">{{item.name}}</div>
                    <div class="price">
                        ￥{{item.price}}
                        <span class="ori_price">￥{{item.ori_price}}</span>
                    </div>
                </div>
                <div class="right">
                    <div class="jian" @click="jian(item)">-</div>
                    <div class="count">{{item.count}}</div>
                    <div class="add" @click="add(item)">+</div>
                </div>
            </div>
            <div class="kong" v-show="tasklist.length == 0">购物车太空了，买点儿东西吧</div>
        </div>
        <div class="bottom">
            <div class="shuliang">
                共<span class="total" v-text="total" @="totals"></span>件
            </div>
            <div class="jiage">
                ￥<span class="totalPrice" v-text="totalPrice"></span>
                <span class="ori_totalPrice" v-text="ori_totalPrice">￥</span>
            </div>
        </div>
    </div>
</body>

</html>
<script src="./js/vue-2.5.21.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: {
            tasklist: [
                { id: 1, pic: './img/pic4.png', name: '秋刀鱼/斤', price: '35.00', ori_price: 36, count: 1 },
                { id: 2, pic: './img/pic5.png', name: '鲜大带鱼/条', price: '45.00', ori_price: 46, count: 1 },
                { id: 3, pic: './img/pic6.png', name: '鲜活全母皮皮虾/斤', price: '85.00', ori_price: 86, count: 1 },
            ],
            total: '3',
            totalPrice: '165',
            ori_totalPrice: '168'
        },
        methods: {
            jian(item) {
                if (item.count > 1) {
                    item.count--
                    this.total--
                    this.totals()
                }
            },
            add(item) {
                if (item.count < 5) {
                    item.count++
                    this.total++
                    this.totals()
                }
            },
            empty() {
                this.tasklist = []
                this.total = 0
                this.totalPrice = 0
                this.ori_totalPrice = 0
            },
            totals() {
                this.totalPrice = this.tasklist.reduce((prev, item) => {
                    return prev + (item.count * item.price)
                }, 0)
                this.ori_totalPrice = this.tasklist.reduce((prev, item) => {
                    return prev + (item.count * item.ori_price)
                }, 0)
                console.log(this.total);
            }
        }
    })
</script>